﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
using System.Data.SqlClient;
using System.Configuration;

namespace ivv
{
    public partial class esqueciSenha : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtEmail.Focus();
        }

        protected void btnEnviar_Click(object sender, EventArgs e)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();

            // carregar locais de origem para o ComBox
            string sqla = "select * from usuario where emailUsuario = '" + txtEmail.Text + "'";

            SqlCommand cmd = new SqlCommand(sqla, sqlc);
            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.HasRows)
            {
                dr.Read();

                MailMessage email = new MailMessage("testmanage4systems@gmail.com", txtEmail.Text);

                email.Subject = "Recuperação de senha - FIEPE - " + DateTime.Now.ToString("dd/MM/yyyy");
                email.Body = "<table><tr><th>Nome do usuário</th><th>" + dr["nomeUsuario"].ToString() + "</th></tr>";
                email.Body = "<tr><th>Senha</th><th>" + dr["senhaUsuario"].ToString() + "</th></tr></table>";
                email.IsBodyHtml = true;

                SmtpClient smtpClient = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587
                };

                NetworkCredential credencial = new NetworkCredential("testmanage4systems@gmail.com", "m40s2013");

                smtpClient.Credentials = credencial;

                smtpClient.Send(email);

                //mensagem java script
                string literalcc = "<script language=JavaScript>finalizar();</script>";
                pnlJS.Controls.Add(new LiteralControl(literalcc));
            }
            else
            {
                //mensagem java script
                string literalcc = "<script language=JavaScript>finalizarErro();</script>";
                pnlJS.Controls.Add(new LiteralControl(literalcc));
            }
            // fim do carregamento
            dr.Close();
            cmd.Dispose();

            sqlc.Close();
        }
    }
}
